#include <string.h>
#include <stdio.h>
#include "mhscpu.h"
#include "mh_sha.h"
#include "mh_bignum_tool.h"
#include "debug.h"

void Sha_test()
{
	uint16_t  i;
	sha1_context ctx;
	sha2_context ctx2;
	
	uint8_t buf0[0x30];
	uint8_t buf1[0x30];
	uint8_t t0[2] = "12";
	uint8_t t1[2] = "12";
	uint8_t t2[2] = "12";
	uint8_t t3[2] = "12";
	uint8_t t4[2] = "12";
	
	unsigned char TestSHA[100] = "0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789";
	uint8_t input[20] = "wangzhanbei";
	uint8_t digest[32] = {0};
	printf("Test the sha function...\r\n");
    mh_sha1_starts(&ctx);
	mh_sha1_update(&ctx,t0, 2);
	mh_sha1_update(&ctx,t0, 2);
	mh_sha1_update(&ctx,t0, 2);
	mh_sha1_update(&ctx,t0, 2);
	mh_sha1_update(&ctx,t0, 2);
	mh_sha1_update(&ctx,TestSHA, 100);
	mh_sha1_update(&ctx,t0, 2);

	mh_sha1_update(&ctx,input, 11);
	mh_sha1_update(&ctx,TestSHA, 100);
	mh_sha1_finish(&ctx,digest);
    memset(digest,0,32);


    mh_sha1_starts(&ctx);
	mh_sha1_update(&ctx,TestSHA, 100);
	mh_sha1_update(&ctx,input, 11);
	mh_sha1_finish(&ctx,digest);
    memset(digest,0,32);

	mh_sha256_starts(&ctx2);
	mh_sha256_update(&ctx2,TestSHA, 100);
	mh_sha256_update(&ctx2,input, 11);
	mh_sha256_finish(&ctx2,digest);


	memset(buf0,'a',0x30);
	memset(buf1,'b',0x30);
	memset(digest,0,32);
	mh_sha256_starts(&ctx2);
	mh_sha256_update(&ctx2,buf0, 0x30);
	mh_sha256_update(&ctx2,buf1, 0x30);
	mh_sha256_finish(&ctx2,digest);

    printf("The sha result is \n");
	for(i=0;i<32;i++)
	{
		printf("0x%2x\t",digest[i]);
	}
}

void test_SHA(void)
{
	mh_rng_callback f_rng = mh_rand_p;
	
	const char *cc1_160 = "39F37D135FA66858516859BEF7E32F6C40D23640";
	const char *cc1_224 = "CBAE2F3E2A35AD314C8433509129F1B9AFC902E2BE144AF565CEF4EB";
	const char *cc1_256 = "346A578A87CFFC251AE7B0FE9ED08607C3537B691CFE7F521F6AD7D5AF015D90";
	const char *cc1_384 = "FBEBF75AD47A69F30729A9C2620904AFBD1BBCC237645E0CCA474AAD36320ED008FC5D9E2A2208C5FDF37F967A0BAEE9";
	const char *cc1_512 = "4E5B77FEF46B48EFD195BB5478B82CB70F203BF5600345B9B7C00567444C029E2E85143A321C7E88C558F564B2D856FB4CFB6979F2D37A27A5531E469809AC6A";

	
	const int inputlen = 1024 * 10;
	int i = 0;
	
	unsigned char c_512[10241] = {0};
	unsigned char TestSHA[30] = "012345678901234567890123456789";

	unsigned char c1_160[160 /8] = {0};
//	unsigned char c1_160_tmp[160 /8] = {0};
	unsigned char c1_224[224 /8] = {0};
	unsigned char c1_256[256 /8] = {0};
	unsigned char c1_384[384 /8] = {0};
	unsigned char c1_512[512 /8] = {0};

	unsigned char exp_c1_160[160 /8] = {0};
//	unsigned char exp_c1_160_tmp[160 /8] = {0};
	unsigned char exp_c1_224[224 /8] = {0};
	unsigned char exp_c1_256[256 /8] = {0};
	unsigned char exp_c1_384[384 /8] = {0};
	unsigned char exp_c1_512[512 /8] = {0};
	
	DBG_PRINT("\nSHA Test In\n");
	
	bn_read_string_to_bytes(exp_c1_160, sizeof(exp_c1_160), cc1_160);
	bn_read_string_to_bytes(exp_c1_224, sizeof(exp_c1_224), cc1_224);
	bn_read_string_to_bytes(exp_c1_256, sizeof(exp_c1_256), cc1_256);
	bn_read_string_to_bytes(exp_c1_384, sizeof(exp_c1_384), cc1_384);
	bn_read_string_to_bytes(exp_c1_512, sizeof(exp_c1_512), cc1_512);
	
	
	for(i = 0; i < inputlen; i++)
		c_512[inputlen - i - 1] = i;
	
	memset(c1_160, 0, sizeof(c1_160));
	memset(c1_224, 0, sizeof(c1_224));
	memset(c1_256, 0, sizeof(c1_256));
	memset(c1_384, 0, sizeof(c1_384));
	memset(c1_512, 0, sizeof(c1_512));
	mh_sha(SHA_160, c1_160, c_512, inputlen);
	r_printf(0==(memcmp(c1_160, exp_c1_160, sizeof(exp_c1_160))), "SHA c1_160 test\n");
	mh_sha(SHA_224, c1_224, c_512, inputlen);
	r_printf(0==(memcmp(c1_224, exp_c1_224, sizeof(exp_c1_224))), "SHA c1_224 test\n");
	mh_sha(SHA_256, c1_256, c_512, inputlen);
	r_printf(0==(memcmp(c1_256, exp_c1_256, sizeof(exp_c1_256))), "SHA c1_256 test\n");
	mh_sha(SHA_384, c1_384, c_512, inputlen);
	r_printf(0==(memcmp(c1_384, exp_c1_384, sizeof(exp_c1_384))), "SHA c1_384 test\n");
	mh_sha(SHA_512, c1_512, c_512, inputlen);
	r_printf(0==(memcmp(c1_512, exp_c1_512, sizeof(exp_c1_512))), "SHA c1_512 test\n");
	
	memset(c1_160, 0, sizeof(c1_160));
	memset(c1_224, 0, sizeof(c1_224));
	memset(c1_256, 0, sizeof(c1_256));
	memset(c1_384, 0, sizeof(c1_384));
	memset(c1_512, 0, sizeof(c1_512));
	mh_sha_sec(SHA_160, c1_160, c_512, inputlen, f_rng, NULL);
	r_printf(0==(memcmp(c1_160, exp_c1_160, sizeof(exp_c1_160))), "SHA sec c1_160 test\n");
	mh_sha_sec(SHA_224, c1_224, c_512, inputlen, f_rng, NULL);
	r_printf(0==(memcmp(c1_224, exp_c1_224, sizeof(exp_c1_224))), "SHA sec c1_224 test\n");
	mh_sha_sec(SHA_256, c1_256, c_512, inputlen, f_rng, NULL);
	r_printf(0==(memcmp(c1_256, exp_c1_256, sizeof(exp_c1_256))), "SHA sec c1_256 test\n");
	mh_sha_sec(SHA_384, c1_384, c_512, inputlen, f_rng, NULL);
	r_printf(0==(memcmp(c1_384, exp_c1_384, sizeof(exp_c1_384))), "SHA sec c1_384 test\n");
	mh_sha_sec(SHA_512, c1_512, c_512, inputlen, f_rng, NULL);
	r_printf(0==(memcmp(c1_512, exp_c1_512, sizeof(exp_c1_512))), "SHA sec c1_512 test\n");
	
}
